#!/usr/bin/env bash
ROOT=$(cd "$(dirname "$0")"; pwd)
cd $ROOT

increment_version ()
{
  declare -a part=( ${1//\./ } )
  declare    new
  declare -i carry=1

  for (( CNTR=${#part[@]}-1; CNTR>=0; CNTR-=1 )); do
    len=${#part[CNTR]}
    new=$((part[CNTR]+carry))
    [ ${#new} -gt $len ] && carry=1 || carry=0
    [ $CNTR -gt 0 ] && part[CNTR]=${new: -len} || part[CNTR]=${new}
  done
  new="${part[*]}"
  echo -e "${new// /.}"
}


OLD=`cat $ROOT/version`
VER=$(increment_version $OLD)
echo $VER > $ROOT/version
cd $ROOT
sed -i "/@version/s/$OLD/$VER/g" $ROOT/ver.js
coffee -o tmp.js -b  -c main.coffee
cat ver.js city.js > main.js
# echo -e "const CITY = \c" >> main.js
cat tmp.js >> main.js
rm tmp.js
sync

